package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
Page<SetmealVO > setmealVO= setmealMapper.selectPage(setmealPageQueryDTO);
        Page p=(Page) setmealVO;

        return new PageResult(p.getTotal(),p.getResult());
    }

    @Transactional
    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
    setmeal.setStatus(StatusConstant.DISABLE);
        BeanUtils.copyProperties(setmealDTO,setmeal);
    setmealMapper.insertSetmeal(setmeal);
    log.info("获取id",setmeal.getId());
    List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
    setmealDishes.forEach(setmealDishe ->{
        setmealDishe.setSetmealId(setmeal.getId());});
    setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO = setmealMapper.select(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectById(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.upate(setmeal);
        log.info("获取id",setmealDTO.getId());
   setmealDishMapper.delete(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
      setmealDishes.forEach(setmealDish -> {
          setmealDish.setSetmealId(setmeal.getId());
      });
      setmealDishMapper.insert(setmealDishes);
    }

    @Override
    public void enDisable(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.upate1(setmeal);


    }
    @Transactional
    @Override
    public void delete(List<Long> ids) {

      ids.forEach(id ->{Setmeal setmeal=setmealMapper.selectById(id);
          if (setmeal.getStatus()==StatusConstant.ENABLE){
              throw new RuntimeException(MessageConstant.SETMEAL_ON_SALE);
          } });
        ids.forEach(setmealId ->{

            setmealMapper.deleteById(setmealId);
            setmealDishMapper.delete1(setmealId);
        });

    }

    @Override
    public List<Setmeal> selectByCtageoryId(Setmeal setmeal) {
    List<Setmeal> setmealList= setmealMapper.selectByCtaegoryId(setmeal);
        return setmealList;
    }

    @Override
    public List<DishItemVO> getById(Long id) {
     List<DishItemVO> itemVOList=   setmealMapper.getDishItemBySetmealId(id);
        return itemVOList;
    }
}
